(function(){
	$(function(){
		function easyUploader(box){
			var config = {
				$button:box.find('.upload-button'),
				$picArea:box.find(''),
				$uploader:box.find('.uploader'),
				$viewImgTpl:box.find('.id-photo-img'),
				$inputTpl:box.find("input[type='hidden']"),
				imgType:''
			};
			var loader = BMXimg.init(config);
			loader.then(function(d,$e){
				if(d.state==200){
					//console.log($e);
					console.log(config.$viewImgTpl,d.data['url'])
					config.$viewImgTpl.attr('src', d.data['url']);
					config.$inputTpl.val(d.data.photoId);
				}else{
					alert('上传失败')
				}
			});
		}
		
		window.easyUploader = easyUploader;
		console.log(easyUploader)
		//uploadAllGo()
		
	})
})();

//地址联动
(function(){
	//表单名字
	var formName = 'address_id'
	$(document).ready(function() {
		var box = $('.js-select-address-box')
		var tpl = $('.js-select-address').remove()
		var optionTpl = $("<option></option>");
		var currentId = 0;
		function getSonIds(){
			api.solo('area',[currentId],function(d){
				var list = d.data.list;
				var length = list.length;
				if(length<1) {
					//最后一个加个名字
					$('.js-select-address:last').attr('name',formName)
					return;
				}
				var newSelect = tpl.clone()
				for(var i=0;i<length;i++){
					var thisOption = optionTpl.clone();
					thisOption.val(list[i]['id']).html(list[i]['name'])
					newSelect.append(thisOption)
				}
				//特殊设置
				if(currentId==0) addNoAddress(newSelect)
				box.append(newSelect)
				currentId = list[0]['id']
				getSonIds()
			})
		}
		//初始化
		getSonIds()
		box.on('change', '.js-select-address', function(event) {
			console.log('change')
			currentId = $(this).val();
			$(this).nextAll().remove()
			getSonIds()
		});

		//特殊化 第一条
		function addNoAddress(that){
			var specialOption = optionTpl.clone().val(-1).html('其他地区')
			that.append(specialOption)
		}
		
	});
})()


$(function(){
    //百度地图API功能
    var map = new BMap.Map("allmap");
    map.addControl(new BMap.NavigationControl());
    map.enableScrollWheelZoom();    //启用滚轮放大缩小
    map.enableContinuousZoom();    //启用地图惯性拖拽
    //map.setZoom(10);
	api.solo( "siteInfo", [], function(d){
		var siteInfo= d.data;
		map.centerAndZoom(new BMap.Point(siteInfo.lon,siteInfo.lat),11);
	});
    var point = new BMap.Point(117.565297,33.547786);
    map.centerAndZoom(point,13);
    var marker = new BMap.Marker(point);
    map.addOverlay(marker);
    marker.enableDragging();
    marker.addEventListener("dragend", function(e){
        var p = e.point;
        $("[name=latitude]").val(p.lat);
        $("[name=longitude]").val(p.lng);
    });
});
